home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group98b.txt / 000124_icon-group-sender _Wed Jul 8 14:01:48 1998.msg < prev    next >
Internet Message Format  |  2000-09-20  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: from kingfisher.CS.Arizona.EDU (kingfisher.CS.Arizona.EDU [192.12.69.239])
  3.     by baskerville.CS.Arizona.EDU (8.8.8/8.8.7) with SMTP id OAA02640
  4.     for <icon-group-addresses@baskerville.CS.Arizona.EDU>; Wed, 8 Jul 1998 14:01:47 -0700 (MST)
  5. Received: by kingfisher.CS.Arizona.EDU (5.65v4.0/1.1.8.2/08Nov94-0446PM)
  6.     id AA10420; Wed, 8 Jul 1998 14:01:34 -0700
  7. From: "Nevin :-] Liber" <nevin@pendragon-software.com>
  8. To: <icon-group@optima.CS.Arizona.EDU>
  9. Cc: "David Starner" <dstarner98@aasaa.ofe.org>
  10. Subject: Re: Stripping excessive spaces
  11. Date: Tue, 7 Jul 1998 19:55:41 -0500
  12. Message-Id: <01bdaa0b$2191b000$c37546cf@NEVIN>
  13. Mime-Version: 1.0
  14. Content-Type: text/plain;
  15.     charset="iso-8859-1"
  16. Content-Transfer-Encoding: 7bit
  17. X-Priority: 3
  18. X-Msmail-Priority: Normal
  19. X-Mailer: Microsoft Outlook Express 4.71.1712.3
  20. X-Mimeole: Produced By Microsoft MimeOLE V4.71.1712.3
  21. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  22. Status: RO
  23. Content-Length: 2099
  24.  
  25.  
  26. >I've just started programming in Icon, and I came across the problem of
  27. >removing all the starting and ending spaces from a string. This was my
  28. >solution, but I think it's terribly inefficent. Can some one help me
  29. >with a better way to do this?
  30.  
  31. The built-in function trim() does the bulk of what you want to do.  Check
  32. out the documentation at
  33. <http://www.cs.arizona.edu/icon/refernce/funcsz.htm#trim>.
  34.  
  35. If you are looking to express the algorithm as a one-liner, the following
  36. should suffice:
  37.  
  38.  
  39.     procedure clip(s, c)
  40.  
  41.         /c := ' '
  42.  
  43.         s := string(s) | runerr(103, s)
  44.         c := cset(c) | runerr(104, c)
  45.  
  46.         return reverse(trim(reverse(trim(s, c)), c))
  47.  
  48.     end
  49.  
  50. If the "inefficiency" of reversing a string twice bothers you, you might
  51. wish to write it as:
  52.  
  53.     procedure clip(s, c)
  54.  
  55.         /c := ' '
  56.  
  57.         s := string(s) | runerr(103, s)
  58.         c := cset(c) | runerr(104, c)
  59.  
  60.         trim(s, c) ? {
  61.             tab(many(c))
  62.             return tab(0)
  63.         }
  64.  
  65.     end
  66.  
  67. These are both a little more general than what you asked for, as you can
  68. clip any arbitrary cset (which defaults to space) instead of just spaces.
  69. Also, this behaves a little differently than your algorithm does, as it
  70. aborts with a runtime error instead of just failing if s is not convertible
  71. to a string (and c is not null and not convertible to a cset).
  72.  
  73. If you wanted to generalize this a little more, say to use different csets
  74. for the left and right sides (defaulting the left clip to space if none is
  75. provided, and defaulting the right clip to be the same as the left clip if
  76. non is provided), you might write it as:
  77.  
  78.     procedure clip(s, cLeft, cRight)
  79.  
  80.         /cLeft := ' '
  81.         /cRight := cLeft
  82.  
  83.         s := string(s) | runerr(103, s)
  84.         cLeft := cset(cLeft) | runerr(104, cLeft)
  85.         cRight := cset(cRight) | runerr(104, cRight)
  86.  
  87.         trim(s, cRight) {
  88.             tab(many(cLeft))
  89.             return tab(0)
  90.         }
  91. __
  92.  Nevin ":-)" Liber  <mailto:nevin@pendragon-software.com>  (847) 816-9926
  93.   Senior Software Engineer, Pendragon Software <http://www.webfayre.com/>
  94.  
  95.